#!groovy

def call(String jenkinsLabel) {
    echo jenkinsLabel
    node("${jenkinsLabel}") {
        def KUTUI_GROUP="https://push.xuthus.cc/group"
        def GIT_URL = 'https://gitee.com/crowdmall/test7.git'
        def ARTI_IPPORT = '10.21.1.105:8083'
        def GIT_CREDENTIALS_ID = 'crowdmall'
        withCredentials([usernamePassword(credentialsId: '1c6eb9ba-7a49-4611-89b3-35feb3c80725', passwordVariable: 'password', usernameVariable: 'username')]) {
            sh "docker login -u ${username} -p ${password} ${ARTI_IPPORT} "
        }
        try {
            docker.image(ARTI_IPPORT+'/docker-local/flyway-yiyun').inside("-v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker --user root") {
                stage('SCM') {
                    git url: GIT_URL,
                        branch: env.BRANCH_NAME,
                        changelog: true,
                        credentialsId: GIT_CREDENTIALS_ID
                }
                stage('modify db') {
                    if (env.BRANCH_NAME == "master") {
                        sh '''
                            rm -rf /flyway-6.4.4/conf/flyway.conf && rm -rf /flyway-6.4.4/sql/ && cp db/flyway.conf /flyway-6.4.4/conf/flyway.conf && cp -r db/sql/ /flyway-6.4.4/sql/
                            /flyway-6.4.4/flyway clean && /usr/bin/flyway-yiyun
                        '''
                    }
                }
                withCredentials([string(credentialsId: 'kutui-token', variable: 'KUTUI_KEY')]) {
                    echo "完成构建，发qq"
                    sh "curl ${KUTUI_GROUP}/${KUTUI_KEY}?c=构建状态:SUCCESS:db-${env.BRANCH_NAME}"
                }
            }
        }catch(exception1){
            withCredentials([string(credentialsId: 'kutui-token', variable: 'KUTUI_KEY')]) {
                echo "构建异常，发qq"
                sh "curl ${KUTUI_GROUP}/${KUTUI_KEY}?c=构建状态:FAILED:db-${env.BRANCH_NAME}"
            }
            throw exception1
        }
    }
}